package com.njtoyo.taxi.admin.rest.controller.business;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.rest.presenter.business.CompanyFeeRatePresenter;
import com.njtoyo.taxi.admin.rest.presenter.business.CompanyListPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.company.*;
import com.njtoyo.taxi.admin.service.business.CompanyService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value = "公司", tags = "公司")
@RequestMapping("business/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @GetMapping("/all")
    @ApiOperation(value = "全部公司", notes = "只返回账号下的数据，无接口权限校验")
    RestResult getAll(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser, CompanyQueryWrapper wrapper) {
        return companyService.getAll(adminUser, wrapper);
    }

    @GetMapping("")
    @ApiOperation(value = "公司列表", notes = "公司列表,权限:companies_list")
    @PermissionScope(names = {"companies_list"})
    RestResult<Page<CompanyListPresenter>> companies(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser, @Valid CompanyListWrapper wrapper) {
        return companyService.companies(adminUser, wrapper);
    }

    @PostMapping("")
    @ApiOperation(value = "新增公司", notes = "新增公司,权限:companies_add")
    @PermissionScope(names = {"companies_add"})
    RestResult addCompany(@Valid @RequestBody CompanyAddWrtapper wrapper) {
        return companyService.addCompany(wrapper);
    }

    @PatchMapping("/{id}")
    @ApiOperation(value = "修改公司基本信息", notes = "修改公司基本信息,权限:companies_update_basic")
    @PermissionScope(names = {"companies_update_basic"})
    RestResult updateCompanyBasic(@PathVariable("id") Long id, @Valid @RequestBody CompanyUpdateWrapper wrapper) {
        return companyService.updateCompanyBasic(id, wrapper);
    }

    @GetMapping("/{id}/fee-rate")
    @ApiOperation(value = "公司手续费", notes = "公司手续费,权限:company_fee_rate,update_company_fee_rate")
    @PermissionScope(names = {"company_fee_rate", "update_company_fee_rate"})
    RestResult<List<CompanyFeeRatePresenter>> companyFreeRateList(@PathVariable("id") Long companyId) {
        return companyService.companyFreeRateList(companyId);
    }

    @PutMapping("/{id}/fee-rate")
    @ApiOperation(value = "修改手续费配置", notes = "修改手续费配置,权限:update_company_fee_rate")
    @PermissionScope(names = {"update_company_fee_rate"})
    RestResult companFreeRateupdate(@PathVariable("id") Long companyId,
                                    @Valid @RequestBody CompanFreeRateWrapper wrapper) {
        return companyService.companFreeRateupdate(companyId, wrapper);
    }

    /**
     * 信息服务费
     *
     * @param companyId
     * @return
     */
    @GetMapping("/infoServiceList")
    RestResult infoService(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                           @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
                           @RequestParam(value = "companyId") Integer companyId,
                           @RequestParam(value = "selectStartDate",required = false) String selectStartDate,
                           @RequestParam(value = "selectEndDate",required = false) String selectEndDate) {
        return companyService.infoServiceById(current, size, companyId, selectStartDate,selectEndDate);
    }

}
